<template>
  <div>
    <h1>品牌列表</h1>
    <div>
      <el-form :inline="true" :model="form" class="demo-form-inline">
        <el-form-item label="品牌名称">
          <el-input
            v-model="form.keyWord"
            placeholder="请输入品牌名称"
            clearable
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="getBrand()">查询</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-button type="primary" @click="showbrand">添加品牌</el-button>

    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="brandId" label="序号" />
      <el-table-column prop="brandName" label="品牌名称" />
      <el-table-column prop="brandUse" label="使用商品数量" />
      <el-table-column prop="brandTop" label="排序" />
      <el-table-column prop="brandStatus" label="状态">
        <template #default="scope">
          <el-switch
            v-model="scope.row.brandStatus"
            active-text="显示"
            inactive-text="隐藏"
            :active-value="1"
            :inactive-value="0"
            @change="onupdateStatus(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" />
    </el-table>
    <div class="demo-pagination-block">
      <el-pagination
        v-model:current-page="form.pageNum"
        v-model:page-size="form.pageSize"
        :page-sizes="[2, 3, 4, 5]"
        :background="true"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>

  <div>
    <el-dialog v-model="dialogFormVisible" title="添加品牌" width="500">
      <el-form
        :model="brandData"
        ref="ruleFormRef"
        style="max-width: 600px"
        :rules="rules"
      >
        <el-form-item
          label="商品名称"
          :label-width="formLabelWidth"
          prop="brandName"
        >
          <el-input v-model="brandData.brandName" autocomplete="off" />
        </el-form-item>
        <el-form-item
          label="排序"
          :label-width="formLabelWidth"
          prop="brandTop"
        >
          <el-input v-model="brandData.brandTop" autocomplete="off" />
        </el-form-item>
        <el-form-item
          label="状态"
          :label-width="formLabelWidth"
          prop="brandStatus"
        >
          <el-radio-group v-model="brandData.brandStatus">
            <el-radio value="1" size="large">显示</el-radio>
            <el-radio value="0" size="large">隐藏</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="submitForm(ruleFormRef)">
            添加
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup>
import { onMounted, ref } from "vue";
import { getBrandList, updateStatusById, addBrand } from "../api/api";
import { ElMessage } from "element-plus";
import type { FormInstance, FormRules } from "element-plus";
import type { ComponentSize } from "element-plus";
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";
const tableData = ref([]);
const ruleFormRef = ref<FormInstance>();
const size = ref<ComponentSize>("default");
const background = ref(false);
const disabled = ref(false);
const total = ref(0);
const form = ref({
  pageNum: 1,
  pageSize: 2,
  keyWord: "",
});
const brandData = ref({
  brandId: null,
  brandName: "",
  brandStatus: null,
  brandTop: null,
});
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log("submit!");
      addBrand(brandData.value).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("添加成功");
          dialogFormVisible.value = false;
          getBrand();
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
};
const rules = ref<FormRules>({
  brandName: [{ required: true, message: "品牌名称不能为空", trigger: "blur" }],
  brandTop: [
    { required: true, message: "排序不能为空", trigger: "blur" },
    { pattern: /^\d+$/, message: "排序只能是数字", trigger: "blur" },
  ],
  brandStatus: [
    {
      required: true,
      message: "状态不能为空",
      trigger: "change",
    },
  ],
});

const showbrand = () => {
  dialogFormVisible.value = true;
  brandData.value = {
    brandId: null,
    brandName: "",
    brandStatus: null,
    brandTop: null,
  };
};
const onupdateStatus = (row: any) => {
  updateStatusById(row).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("修改成功");
      getBrand();
    }
  });
};
const getBrand = () => {
  getBrandList(form.value).then((res) => {
    if (res.data.code == 200) {
      tableData.value = res.data.data.records;
      total.value = res.data.data.total;
    }
  });
};
const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  form.value.pageSize = val;
  getBrand();
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  form.value.pageNum = val;
  getBrand();
};
onMounted(() => {
  getBrand();
});
</script>
